<?php
namespace app\admin\controller\account;

use app\admin\BaseController;
use app\lib\exception\FailedException;
use app\lib\exception\ParameterException;
use think\Request;

class Carousel extends BaseController
{
    /**
     * 轮播图列表
     */
    public function carouselList(){
        $carousel = (new \app\admin\model\Carousel())->selectList([],"id,img_path,type,number","type");
        $carousel = $carousel->toArray();
        foreach ($carousel as $key=>$value){
              $carousel[$key]['img_path'] = serializeMysql($value['img_path'],1);
              if(count($carousel[$key]['img_path'])>0){
                  foreach ($carousel[$key]['img_path'] as $ke=>$val){
                      $carousel[$key]['img_path'][$ke]= imgUrlToAbsolute($val);
                  }
              }
        }
        return  writeJson(200,['carousel'=>$carousel]);
    }

    /**
     * 轮播图修改
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws ParameterException
     */

    public function carouselEdit(Request $request)
    {
        $post = $request->post();
        if(!isset($post['carousel'])){
            throw new ParameterException(['msg'=>'缺少数组']);
        }
        foreach ($post['carousel'] as $key=>$value){
            $post['carousel'][$key]['img_path'] = serializeMysql($value['img_path']);
        }
        $new = $post['carousel'];
        $res  = (new \app\admin\model\Carousel())->saveAll($new);
        if(!$res){
            throw new FailedException(['msg'=>'操作失败']);
        }
        return writeJson(200,[],'修改成功');
    }

}